#pragma once
//============================================================================
// Copyright (C) 2013 Brett R. Jones
//
// Issued to public domain 2013
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================

#include <string>
#include <vector>
#include "VxDefs.h"

#ifndef RCODE
#define RCODE long
#endif

#define VXFILE_TYPE_UNKNOWN				0
#define VXFILE_TYPE_DIRECTORY			1
#define VXFILE_TYPE_VIDEO				2
#define VXFILE_TYPE_AUDIO				3
#define VXFILE_TYPE_IMAGE				4
#define VXFILE_TYPE_GENERIC				5

#define FILE_TYPE_MASK					0x00ff		
#define FILE_TYPE_PHOTO					0x0001		
#define FILE_TYPE_AUDIO					0x0002		
#define FILE_TYPE_VIDEO					0x0004		
#define FILE_TYPE_EXECUTABLE			0x0008	
#define FILE_TYPE_ARCHIVE_OR_DOC		0x0010
#define FILE_TYPE_CD_IMAGE				0x0020
#define FILE_TYPE_OTHER					0x0040
#define FILE_TYPE_ANY					0x007F

#define IMAGE_FILE_EXTENTIONS			"jpg,jpeg,bmp,tga,png,gif,pcx"				
#define AUDIO_FILE_EXTENTIONS			"mp3,wav,wma,ogg"				
#define VIDEO_FILE_EXTENTIONS			"asf,mpg,mpeg,mp4,3gp,mov,avi,divx,mkv,wmv,rm"				
#define EXECUTABLE_FILE_EXTENTIONS		"exe,com"				
#define ARCHIVE_OR_DOC_FILE_EXTENTIONS	"zip,rar,tar,doc,txt,htm,html,pdf"	
#define CD_IMAGE_FILE_EXTENTIONS		"iso,cue,ccd,img,sub,bin,mds,nrg,pdi,mds,vob"	
#define COMBINED_FILE_EXTENTIONS		"jpg,jpeg,bmp,tga,png,gif,pcx,mp3,wav,wma,ogg,asf,mpg,mpeg,mp4,3gp,mov,avi,divx,mkv,wmv,rm,exe,com,zip,rar,tar,doc,txt,htm,html,pdf,iso,cue,ccd,img,sub,bin,mds,nrg,pdi,mds,vob"	

#define FILE_TYPE_PHOTO_STR				"Photo"		
#define FILE_TYPE_AUDIO_STR				"Audio"		
#define FILE_TYPE_VIDEO_STR				"Video"		
#define FILE_TYPE_EXECUTABLE_STR		"Executable"	
#define FILE_TYPE_ARCHIVE_OR_DOC_STR	"Archive or Document"
#define FILE_TYPE_CD_IMAGE_STR			"CD or DVD Image"
#define FILE_TYPE_OTHER_STR				"Other"
#define FILE_TYPE_ANY_STR				"Any"

//============================================================================
bool IsImageFile( std::string& cs );
bool IsImageFileExtention( const char * pExt );
bool IsAudioFile( std::string& cs );
bool IsAudioFileExtention( const char * pExt );
bool IsVideoFile( std::string& cs );
bool IsVideoFileExtention( const char * pExt );
bool IsExecutableFile( std::string& cs );
bool IsExecutableFileExtention( const char * pExt );
bool IsArchiveOrDocFile( std::string& cs );
bool IsArchiveOrDocFileExtention( const char * pExt );
bool IsCdImageFile( std::string& cs );
bool IsCdImageFileExtention( const char * pExt );
bool IsOtherFile( std::string& cs );
bool IsOtherFileExtention( const char * pExt );
//============================================================================

class VxFileInfo : public std::string
{
public:
	//=== vars ===//
	S64				m_s64FileLen;
	U32				m_u32Attributes;
	U32				m_u32FileType;

	//=== constructor ===//
	VxFileInfo(){ m_s64FileLen=0; m_u32Attributes=0; m_u32FileType=VXFILE_TYPE_UNKNOWN;}
	//! equal operator
	VxFileInfo& operator=(const VxFileInfo& oInfo) 
	{	std::string::operator =( oInfo ); 
		m_u32Attributes		= oInfo.m_u32Attributes;
		m_s64FileLen		= oInfo.m_s64FileLen;
		m_u32FileType		= oInfo.m_u32FileType;
		return *this;
	}
	VxFileInfo& operator=(const std::string& str) { std::string::operator =(str); return *this;}
	VxFileInfo& operator=(const char * str) { std::string::operator =(str); return *this;}
	VxFileInfo& operator+=(const std::string& str) { std::string::operator +=(str); return *this;}
	VxFileInfo& operator+=(const char * str) { std::string::operator +=(str); return *this;}

};

class VxFileFinder
{
public:
	bool m_bAbort;

	void Abort( void ){ m_bAbort = true; }

	//find files matching the extension (file type) list
	RCODE FindFilesByExtension(	std::string					csPath,								//start path to search in
								std::vector<std::string> &	acsExtensionList,					//Extensions of files to find
								std::vector<VxFileInfo> &	aoFileList,							//return FileInfo in array
								bool						bRecurse = false,					//recurse subdirectories if true
								bool						bUseFilterListToExclude = false		//if true dont return files matching filter else return files that do
								);
	//find files matching the extension (file type) list
	RCODE FindFilesByName(		std::string					csPath,								//start path to search in
								std::vector<std::string> &	acsWildNameList,					//Wildcard Name match strings
								std::vector<VxFileInfo> &	aoFileList,							//return FileInfo in array
								bool						bRecurse = false,					//recurse subdirectories if true
								bool						bUseFilterListToExclude = false		//if true dont return files matching filter else return files that do
								);
protected:
	bool HasSameExtension( std::string csCurrentNode,
							std::vector<std::string> &acsExtensionList );
	bool HasMatchingName( std::string csCurrentNode,
							std::vector<std::string> &acsWildNameList );
};

//! search for files
RCODE VxFindFilesByExtension(	std::string					csPath,							//start path to search in
								std::vector<std::string>&	acsExtensionList,				//Extensions ( file extentions )
								std::vector<VxFileInfo>&	aoFileList,						//return FileInfo in array
								bool						bRecurse = false,				//recurse subdirectories if true
								bool						bUseFilterListToExclude = false	//if true dont return files matching filter else return files that do
								);

RCODE VxFindFilesByName(	std::string					csPath,							//start path to search in
							std::vector<std::string>&	acsWildNameList,				//filters
							std::vector<VxFileInfo>&	aoFileList,						//return FileInfo in array
							bool						bRecurse = false,				//recurse subdirectories if true
							bool						bUseFilterListToExclude = false	//if true dont return files matching filter else return files that do
							);




